<template>
  <div class="save-or-edit">
    <el-form ref="roleForm" :model="roleForm" :rules="roleRules">
      <el-form-item label="角色名称" prop="name" :label-width="formLabelWidth">
        <el-input
          placeholder="请填写角色名称"
          v-model="roleForm.name"
          autocomplete="off"
        ></el-input>
      </el-form-item>
      <el-form-item label="角色编码" prop="code" :label-width="formLabelWidth">
        <el-input
          placeholder="请填写角色编码"
          v-model="roleForm.code"
          autocomplete="off"
        ></el-input>
      </el-form-item>
      <el-form-item
        placeholder="请填写角色描述内容"
        label="角色描述"
        :label-width="formLabelWidth"
      >
        <el-input
          type="textarea"
          :rows="5"
          v-model="roleForm.description"
          autocomplete="off"
        ></el-input>
      </el-form-item>
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button @click="cancelAddOrEditRole">取 消</el-button>
      <el-button type="primary" @click="confirmAddOrEditRole('roleForm')"
        >确 定</el-button
      >
    </div>
  </div>
</template>

<script>
import { addRole, getRoleById } from '@/service/role'

export default {
  name: 'SaveOrEdit',
  props: {
    roleId: {
      type: [Number, String],
      default: ''
    },
    isEdit: {
      type: Boolean,
      default: false
    }
  },
  computed: {},
  data () {
    return {
      roleForm: {
        name: '',
        code: '',
        description: ''
      },
      formLabelWidth: '120px',
      roleRules: {
        name: [{ required: true, message: '请输入角色名称', trigger: 'blur' }],
        code: [{ required: true, message: '请输入角色编码', trigger: 'blur' }]
      }
    }
  },
  created () {
    this.roleId && this.loadRole(this.roleId)
  },
  watch: {
    roleId (newValue) {
      // console.log(newValue)
      if (this.isEdit) {
        newValue && this.loadRole(newValue)
      }
    }
  },
  methods: {
    //   加载用户信息
    async loadRole (roleId) {
      // console.log(roleId)
      if (!this.roleId) return
      const { data } = await getRoleById(roleId)
      if (data.code === '000000') {
        this.roleForm = data.data
        // for (const key in data.data) {
        //   if (data.data[key]) {
        //     this.roleForm[key] = data.data[key]
        //   }
        // }
      }
    },
    // 保存编辑结果
    async confirmAddOrEditRole (formName) {
      try {
        // 校验是否有未填项
        const result = await this.$refs[formName].validate()
        // 有必填项未填，中止操作
        if (!result) return
        const { data } = await addRole(this.roleForm)
        if (data.code === '000000') {
          this.$message.success(this.isEdit ? '角色编辑成功' : '角色新增成功')
          // 获取新的角色列表
          this.$emit('success')
          //   置空用户信息
          this.roleForm = {}
          this.roleId = ''
        }
      } catch (error) {
        console.log('验证失败', error)
      }
    },
    // 取消角色编辑
    cancelAddOrEditRole () {
      this.$emit('cancel')
      //   置空用户信息
      this.roleForm = {}
    }
  }
}
</script>

<style></style>
